Applescript 4 NetAdmin
Volume Number: 15
Issue Number: 8
Column Tag: Network Administration
AppleScript for Network Administrators
by Russ Coffman
Edited by Todd Staufer, Technical Edit by Cal Simone
Getting Started
Introduction
You may have programmed in BASIC, HyperCard, or C, or not at all. I had done all that
plus used UserLand's Frontier scripting system, but for some reason, I was slow to get
into AppleScript. Now I can't stop. If you have thought of getting into AppleScripting
but haven't yet, here are some reasons and ways to get started, especially if you are a
network administrator.
Like anything, motivation is important in learning. It helps to have a particular
problem in mind before starting. But that requires knowing what AppleScript can do.
That turns out to be a lot. For a quick start on its capabilities, visit Apple's
AppleScript web page at http://www.apple.com/applescript/
Luckily, there are almost limitless applications for network administrators within
the scope of AppleScript. Here are a few simple ones I found when starting off:
Simple but tedious tasks. An early script I wrote just deleted the
contents of folders on file servers used for backups. Users would drag files to
be backed up to their backup folder, but that tended to fill up the server (since
replaced with G3s). Opening each folder, selecting all the items in the folder,
and dragging them to the Trash was tedious. AppleScript can process all folders
in one drag and drop. This was a simple script, but did the job.
Repetitive tasks. Another simple script opened 1800 WordPerfect
documents and saved them as plain text files for input into an archiving and
indexing system. Sounds easy, but getting footnotes out too required some
tricks. Several days of manual labor was done in a few hours, error-free.
Cleaning up after an install, or doing complete installs. A lot
of software can be safely drag-installed -usually faster than running an
installer, plus you can make new aliases in the Apple menu, save and restore
user files to the right place, and in general customize the installation to your
users' needs. System installs often install unneeded files - use scripts to keep
your System Folders lean and clean.
Periodic operations. Examples are backing up an important file or
folder, or watching for some network condition and being alerted to it. We had
a problem with our 4-Sight fax server application dying. Luckily it was
scriptable (but the documentation and scripting addition are hidden on one of
the install disks), and had a command that would just say "I'm here." A
stay-open applet (a script application that doesn't quit after executing) polls
it periodically and alerts me if it does not get a response.
Get Motivated to Learn AppleScript
Those are some general uses for AppleScript, well within its capabilities, and good
learning exercises. Think of your shop and identify potential applications. Start with
at least one real problem and keep it in mind as you learn: this will be Your First
Script.
Now, how do you go about learning AppleScript? Learning AppleScript is much like
learning to play the trombone. The best way to learn the trombone is to play the
trombone. The best way to learn AppleScript is to write scripts! But there's still the
"getting started" thing. You've got your real world problem defined - a big step. Solve
that problem and you've got some positive feedback - "I can do this!" From then on,
you'll never want to stop AppleScripting. The upside is that AppleScripting is much
less annoying to others than playing the trombone. It might even get you a raise, or
even a new career. It certainly looks good on your résumé.
Getting Started
You don't need much to get started. Apple's Script Editor is included with every Mac. If
you plan to get gung-ho with AppleScript, you may want to invest early on in an
editor/debugger, such as Main Event's Scripter®, or Late Night Software's Script
Debugger. Both of these tools let you step through scripts one command at a time, see
the result of each command, watch variables change, and much more. You need two
more things to get started: a good book and a good learning environment.
Get a book (or better, several books) on AppleScript. Like many people, I started with
Danny Goodman's AppleScript Handbook, now in print again, but somewhat out of date.
No matter, it's a good intro. See the list of reference material at the end of this article
for more good books. Whichever book you choose, be determined to start at page one and
work to the end, skipping nothing.
Do all the examples and exercises. It's important to type in the examples, don't just
load them from a disk if one is provided. The exercise of typing in a script makes you
think through what you are doing. Soon you'll notice you can anticipate what's next
without even looking at the next line - you'll go into "auto-complete" mode! Trust me
on this, it works. Just watch for for that "I can do this" light bulb to light up above
your now-swelled head.
People learn best in different ways. You may do best by setting aside an hour a day with
a goal of getting through one chapter or one exercise in that hour. Others, like me, do
best with total immersion - at least four uninterrupted hours of study with no
distractions. Have an AppleScript Weekend. Decide on a regimen that has worked for
you and stick to it. Have plenty of water, coffee, Jolt Cola, and snacks available. You
want to be way up on Maslow's Pyramid so you can concentrate on AppleScript. Set the
mood to what works for you, from total silence to MTV.
Example Scripts
Now let's look at a few scripts. Even if you are new to AppleScript, you'll find it literal
enough to follow along. To help, in the scripts below I've added "comments," indicated
by a double dash:
- my comment blah blah.
First, let's look at deleting the contents of folders dropped on an AppleScript droplet
(As mentioned, an "applet" is a script saved as an application that acts like a real
application. The script can run when double-clicked and it can receive and handle
messages sent from other applications and scripts. It can also process items dropped on
its icon in the Finder - if so, it's a "droplet"):
on open fileList
-sets the variable fileList to a list of folders dropped
Any AppleScript applet becomes "droppable" in the Finder just by including an "on
open" handler (a handler in AppleScript is the equivalent of a procedure, function, or
subroutine in other languages - a starting point for a block of commands). In
AppleScript, a handler can respond to a message from another application, in this case,
the "open" handler responds to "open" messages sent from the Finder. A list of
references to files, folders, or disks you drop are passed to a variable, in this case
fileList. A list in AppleScript is a one-dimensional array of the form {item 1, item
2...item n}, where the items can be any type of object - text, numbers, references to